//
// Created by kriko on 2021/7/15.
//

#include "strcliselect01.h"

void str_cli_select(FILE* fp, int sockfd) {
   int maxfpd1;
    fd_set reset;
    char sendline[MAXLINE], recvline[MAXLINE];
   FD_ZERO(&reset);
   while (1) {
       FD_SET(fileno(fp), &reset);
       FD_SET(sockfd, &reset);
       maxfpd1 = fmax(fileno(fp), sockfd) + 1;
       Select(maxfpd1, &reset, NULL, NULL, NULL);

       if(FD_ISSET(sockfd, &reset)) {
           if (read(sockfd, recvline, MAXLINE) == 0) {
               unix_error("str_cli_select: server terminated prematurely");
               return;
           }
           Fputs(recvline, stdout);
       }

       if(FD_ISSET(fileno(fp), &reset)) {
           if(Fgets(sendline, MAXLINE, fp) == NULL) {
               return;
           }
           Write(sockfd, sendline, strlen(sendline));
       }
   }
   return;
}
